﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace FlowersStore
{
	public class UserOrder
	{
		List<ProductPosition> orderPositions=new List<ProductPosition>();
		decimal totalPrice;
		public DateTime deliveryDate;
		public DateTime startTime, endTime;
		public string address;
		public string addInfo;
		public int status = 2;
		public string senderName, 
			receiverName, 
			senderPhone, 
			receiverPhone, 
			senderAddInfo, 
			receiverAddInfo;
		public long id;

		public List<ProductPosition> Positions
		{ get { return orderPositions; } }

		public decimal TotalPrice
		{ get { return totalPrice; } }

		public void AddPosition(ProductPosition pos)
		{
			orderPositions.Add(pos);
			ComputeTotalPrice();
		}

		public List<long> GetProductIds()
		{
			return (from p in orderPositions
					select p.ProductId).ToList();
		}

		public int GetProductQuantity(long productId)
		{
			return (from pos in orderPositions
					where pos.ProductId == productId
					select pos.Quantity).SingleOrDefault();
		}


		void ComputeTotalPrice()
		{
			using (FlowerStoreDataContext db = new FlowerStoreDataContext())
			{
				totalPrice = 0;
				foreach (ProductPosition pos in orderPositions)
				{
					totalPrice += (from p in db.Products
								   where p.Product_id == pos.ProductId
								   select p.Product_price).Single() * pos.Quantity;
				}				
			}
		}
	}
}